package gaml.testing;

import static org.junit.Assert.assertEquals;
import gaml.error.GamlException;
import gaml.java.ClassSerializerStore;
import gaml.java.NodeObjectBridge;
import gaml.java.serializer.ComplexSerializerFactory;
import gaml.java.serializer.collection.ArrayListSerializer;
import gaml.java.serializer.primitive.StringSerializer;
import gaml.testing.ComplexSerializerTest.Bar;
import gaml.yaml.ScalarNode;
import gaml.yaml.SequenceNode;
import gaml.yaml.YamlNode;
import gaml.yaml.ahchor.YamlAnchorCodeStore;
import gaml.yaml.ahchor.YamlAnchorObjectStore;

import java.util.ArrayList;

import org.junit.Test;


public class ArrayListSerializerTest {

	@Test
	public void testDeserialize () throws GamlException {
		SequenceNode node = new SequenceNode (null);
		
		node.addChild (new ScalarNode (node).setValue (".class java.lang.String"));
		
		node.addChild (new ScalarNode (node).setValue ("str1"));
		node.addChild (new ScalarNode (node).setValue ("str2"));
		
		ClassSerializerStore store = new ClassSerializerStore ();
		store.setComplexFactory (new ComplexSerializerFactory (store));
		
		store.register (new StringSerializer ());
		store.register (new ArrayListSerializer (store));
		
		ArrayList <String> list = NodeObjectBridge.create (node, ArrayList.class, store).parseNode (new YamlAnchorCodeStore ()).getObject ();
				
		assertEquals ("str1", list.get (0));
	}

	@Test
	public void testSerialize () throws GamlException {
		ArrayList <String> list = new ArrayList <String> ();
		
		list.add ("str1");
		list.add ("str2");
		list.add ("str3");
		
		ClassSerializerStore store = new ClassSerializerStore ();
		store.setComplexFactory (new ComplexSerializerFactory (store));
		
		store.register (new StringSerializer ());
		store.register (new ArrayListSerializer (store));
		
		YamlAnchorObjectStore anchors = new YamlAnchorObjectStore ();
		
		YamlNode node = NodeObjectBridge.create (list, null, ArrayList.class /*(Class <ArrayList <String>>) list.getClass ()*/, store).parseObject (anchors).getNode ();
		
		assertEquals (4, node.getSequenceChildren ().size ());
		
		for (int i=0; i<3; ++i)
			assertEquals (list.get (i), node.getChild (i + 1).getValue ());
		
//		System.out.println (node.getChild (0).getValue ());
	}
}
